package structure.set.basic.list;

import structure.list.single.SingleLinkedList1;

/**
 * 项目名称: god
 * 包 名 称: com.set.basic.list
 * 类 名 称: MyListSet
 * 类 描 述: TODO
 * 创建时间: 2020/6/14 3:56 下午
 * 创 建 人: Justice
 */
public class MyListSet<E> implements MySet1<E> {
    private SingleLinkedList1<E> list1 = new SingleLinkedList1<E>();

    public int size() {
        return list1.size();
    }

    public boolean isEmpty() {
        return list1.isEmpty();
    }

    public void clear() {
        list1.clear();
    }

    public boolean contains(E element) {
        return list1.contains(element);
    }

    public void add(E element) {
        int index = list1.indexOf(element);
        if (index >= 0) {
            // 存在就覆盖
            list1.set(index, element);
        } else {
            list1.add(element);
        }
    }

    public void remove(E element) {
        int index = list1.indexOf(element);
        if (index >= 0) {
            list1.remove(index);
        }
    }

    public void traversal(Visitor<E> visitor) {
        if (visitor == null) return;
        int size = list1.size();
        for (int i = 0; i < size; i++) {
            if (visitor.visit(list1.get(i))) return;
        }
    }
}
